package com.tm.config;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.tm.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Rocky
 * @create 2022-09-04 18:20
 */
//@Component
public class ZkjZuulFilter extends ZuulFilter {

    @Autowired
    private RedisTemplate redisTemplate;

    //决定过滤器的类型，pre路由之前 routing路由过程中  post路由成功后 error异常时
    public String filterType() {
        return "pre";
    }

    //规定了 过滤器的顺序，默认0 最早执行，数字越小 执行越早
    public int filterOrder() {
        return 0;
    }

    //是否通过过滤
    public boolean shouldFilter() {
        return true;
    }

    //实际上要验证的个中逻辑过滤代码
    public Object run() throws ZuulException {
        //获取到zuul中的请求上下文对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        //获取到request对象和response对象
        HttpServletRequest request = currentContext.getRequest();
        HttpServletResponse response = currentContext.getResponse();

        String userToken = request.getHeader("userToken");
        String phonenumber = request.getHeader("phonenumber");

        Object o1 = redisTemplate.opsForValue().get("token_" + phonenumber);
        if (o1!=null && o1.toString().equals(userToken)){
            currentContext.setSendZuulResponse(true);
            currentContext.setResponseStatusCode(200);
        }else {
            ResponseUtil.outputJSON(response,200);
            currentContext.setSendZuulResponse(false);
        }

        //验证请求不能大于 20M  == 20971520B
        //获取request对象的所有键值对
        /*Class<HttpServletRequest> httpServletRequestClass = HttpServletRequest.class;
        Field[] declaredFields = httpServletRequestClass.getDeclaredFields();
        Long a = 0l;
        //遍历所有键值对
        for (Field declaredField : declaredFields) {
            //declaredField就是每个值
            String name = declaredField.getName();//字段名
            Object o = null;
            try {
                o = declaredField.get(request);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            String s = o.toString();
            int i = name.length() + s.length();
            long l = Long.parseLong(i + "");
            a+=l;
        }

        if(a >= 20971520l){
            response.setHeader("tmMessage","Don't send this garbage request!!!");
            ResponseUtil.outputJSON(response,200);
            currentContext.setSendZuulResponse(false);
        }else{
            //通过response对象，将成功状态传给前端
            //ResponseUtil.outputJSON(response,200);
            //这里的false代表当前的请求不会往后台的微服务中转发
            currentContext.setSendZuulResponse(true);
            currentContext.setResponseStatusCode(200);
        }*/
        return null;
    }

}
